#include "stm32f10x.h"                  // Device header
#include "DELAY.h"

static GPIO_TypeDef* gpiox;
static uint16_t pin;
static uint32_t rcc_APB2Periph;
static u8 isInit = 0,keyNum = 0; //是否初始化、按下的键值

void key_init(uint32_t RCC_APB2Periph,GPIO_TypeDef* GPIOx,uint16_t Pin){
	gpiox = GPIOx;
	pin = Pin;
	rcc_APB2Periph = RCC_APB2Periph;
	RCC_APB2PeriphClockCmd(rcc_APB2Periph,ENABLE);
	GPIO_InitTypeDef type;
	type.GPIO_Mode = GPIO_Mode_IPU; //需要上拉输入，按键未按时默认高电平;
	type.GPIO_Pin =  pin; 
	type.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(gpiox,&type);
	isInit = 1;
}

int key_read(void){
	if(isInit == 1){
		if(GPIO_ReadInputDataBit(gpiox,GPIO_Pin_11) == 0){
			Delay_ms(25); //等待按钮按下的抖动消失
			while(GPIO_ReadInputDataBit(gpiox,GPIO_Pin_11) == 0); //按下后为低电平，等待松手
			Delay_ms(25); //等待按钮松开的抖动消失
			keyNum = 11;
		}else if(GPIO_ReadInputDataBit(gpiox,GPIO_Pin_1) == 0){
			Delay_ms(25); //等待按钮按下的抖动消失
			while(GPIO_ReadInputDataBit(gpiox,GPIO_Pin_1) == 0);
			Delay_ms(25); //等待按钮松开的抖动消失
			keyNum = 1; 
		}
	}
	return keyNum;
}
